﻿using Caliburn.Micro;
using Caliburn.Micro.Recipes.Filters.Framework;
using SS.WPF.Framework.Coroutines;
using SS.WPF.Framework.Display.Dialog;
using SS.WPF.Framework.ExceptionHandling;

namespace SS.WPF.Framework.ViewModel.Container
{
    using System;

    [Rescue("HandleException")]
    public abstract class AllActiveViewModel<T> : Conductor<T>.Collection.AllActive, IExceptionProcessor where T : class, IScreen
    {
        /// <summary>
        /// Gets DialogManager.
        /// </summary>
        public virtual IDialogManager DialogManager
        {
            get
            {
                return IoC.Get<IDialogManager>();
            }
        }

        /// <summary>
        /// Create an instance of LongRunResult
        /// </summary>
        /// <returns></returns>
        public virtual IFluentResult BuildLongRun()
        {
            return IoC.Get<ResultBuilder>().BuildLongRun();
        }

        /// <summary>
        /// Create an instance of ShortRunResult
        /// </summary>
        /// <returns></returns>
        public virtual IFluentResult BuildShortRun()
        {
            return IoC.Get<ResultBuilder>().BuildShortRun();
        }

        /// <summary>
        /// Handle exception
        /// </summary>
        /// <param name="ex"></param>
        public virtual void HandleException(Exception ex)
        {
            var exceptionProcessor = IoC.Get<IExceptionProcessor>();
            var dialogManagerContainer = exceptionProcessor as IDialogManagerContainer;
            if (dialogManagerContainer != null)
            {
                var dialogManager = this.DialogManager;
                dialogManagerContainer.DialogManager = dialogManager;
            }
            exceptionProcessor.HandleException(ex);
        }
    }
}
